﻿using System;
using System.Windows.Markup;
using System.Windows.Controls;

namespace Microsoft.Popfly.GameCreator.GameEngine
{
    public class State
    {
        public Canvas StateObject { get; private set; }

        [Obsolete]
        public double width { get { return Width; } private set { Width = value; } }
        public double Width { get; private set; }

        [Obsolete]
        public double height { get { return Height; } private set { Height = value; } }
        public double Height { get; private set; }

        [Obsolete]
        public CollisionEdges edges { get { return Edges; } private set { Edges = edges; } }
        public CollisionEdges Edges { get; private set; }

        public string Name { get; private set; }
        public string name { get { return Name; } set { Name = value; } }
        
        public string Xaml { get; private set; }
        public string xaml { get { return Xaml; } set { Xaml = value; } }

        public bool IsSolid { get; set; }
        public bool isSolid { get { return IsSolid; } set { IsSolid = value; } }

        public double VisualWidth { get; private set; }
        public double VisualHeight { get; private set; }

        public double VisualXOffset { get; private set; }
        public double VisualYOffset { get; private set; }

        private Data.ActorInfo.StateInfo m_stateInfo;

        public State(string name, Data.ActorInfo.StateInfo stateInfo, Data.Resources resources)
        {
            m_stateInfo = stateInfo;

            Name = name;

            IsSolid = stateInfo.IsSolid;

			string resourceKey = Utils.KeyFromResourceAttribute(stateInfo.Appearance);
			if (resources.ResourceDictionary.ContainsKey(resourceKey))
			{
				Xaml = resources.ResourceDictionary[resourceKey];
			}
            StateObject = Utils.CreateFromXaml(Xaml);

            VisualWidth = stateInfo.Width;
            VisualHeight = stateInfo.Height;

            VisualXOffset = - stateInfo.Edges.Left;
            VisualYOffset = - stateInfo.Edges.Top;

            Width = VisualWidth - (stateInfo.Edges.Left + stateInfo.Edges.Right);
            Height = VisualHeight - (stateInfo.Edges.Top + stateInfo.Edges.Bottom);

            switch (stateInfo.Edges.Type)
            {
                case Microsoft.Popfly.GameCreator.Data.ActorInfo.StateInfo.EdgeInfo.EdgeType.circle:
                    Edges = new CollisionEdges(stateInfo.Width, stateInfo.Height, CollisionEdges.EdgesType.CIRCLE);
                    break;
                case Microsoft.Popfly.GameCreator.Data.ActorInfo.StateInfo.EdgeInfo.EdgeType.rect:
                    Edges = new CollisionEdges(stateInfo.Width, stateInfo.Height, CollisionEdges.EdgesType.RECT);
                    break;
            }
            
        }
    }
}
